﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Reflection;
using System.Drawing;
using System.Text;
using System.IO;
using System.Xml;
using System.Windows.Forms;
using ILog = log4net.ILog;
using log4net;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ZJGIS.EngineControl;
using DevComponents.DotNetBar;

namespace ZJGIS.Framework
{
    public partial class MainFramework1 : Form
    {
        private readonly string layoutFile = Application.StartupPath + "\\layout.config";
        private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        IApplication pApplication = null;
        public MainFramework1()
        {
            InitializeComponent();
       }

        #region EnterCustomize
        private void dotNetBarManager1_EnterCustomize(object sender, EventArgs e)
        {
            MenuCustom customMenu = new MenuCustom();
            string filepath = Application.StartupPath + @"\userlayerout.config";
            DialogResult result = customMenu.ShowDialog(this);
            if (result == DialogResult.OK)
            {
                dotNetBarManager1.SaveLayout(filepath);
            }
            else if (result == DialogResult.Yes)
            {
                if (File.Exists(filepath))
                {
                    try
                    {
                        LoadLayout(filepath);
                    }
                    catch (Exception ex)
                    {
                        log.Error("加载布局文件出错！", ex);
                    }
                }
            }
            dotNetBarManager1.CustomizeClosing();
        }
       

        private void LoadLayout(string filepath)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filepath);
            XmlNode node = xmlDoc.SelectSingleNode("//bars");
            dotNetBarManager1.ToolbarTopDockSite.LoadLayout(node as XmlElement);
            for (int i = dotNetBarManager1.Bars.Count - 1; i >= 0; i--)
            {
                Bar bar = dotNetBarManager1.Bars[i];
                if (bar.Items.Count == 0)
                {
                    bar.Hide();
                }
            }
        }
        #endregion

        #region OnFormClosing
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (e.Cancel == true) return;
            if (MessageBox.Show("您确定要退出系统吗？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    base.OnFormClosing(e);
                    if (e.Cancel == false)
                    {

                        dotNetBarManager1.SaveLayout(layoutFile);
                        //Controller.getController().StopUpdate();
                        Dispose();
                    }
                }
                catch (Exception ex)
                {
                    log.Warn(ex.Message);
                    log.Warn(ex.StackTrace);
                }
            }
            else
            {
                e.Cancel = true;
            }
        }
        #endregion

        #region ButtonClick
        private void buttonItem2_Click(object sender, EventArgs e)
        {
            pApplication.StatusBar.ShowProgressAnimation("ssss", "");
        }

        private void buttonItem3_Click(object sender, EventArgs e)
        {
            pApplication.StatusBar.HideProgressAnimation();
        }

        private void buttonItem4_Click(object sender, EventArgs e)
        {
            pApplication.StatusBar.PlayProgressAnimation(false);

        }

        private void buttonItem5_Click(object sender, EventArgs e)
        {
            Control pControl=(pApplication as IFramework).DockManager.GetControlFromDockPanel("日志");
        }

        private void buttonItem6_Click(object sender, EventArgs e)
        {
            (pApplication as IFramework).DockManager.ActivateDockPanel("测试");
        }

        private void buttonItem7_Click(object sender, EventArgs e)
        {
            pApplication.StatusBar.set_Message(0, "sss测试");

        }

        #endregion
    }
}
